package leetcode15;

import java.util.HashSet;
import java.util.List;

public class test82 {
    /**
     * 139. 单词拆分
     * @param s
     * @param wordDict
     * @return
     */
    public boolean wordBreak(String s, List<String> wordDict) {
        HashSet<String> set = new HashSet<>(wordDict);
        boolean[] valid = new boolean[s.length() + 1];
        valid[0] = true;
        for(int i = 1;i <= s.length();i++){
            for(int j = 0;j < i && !valid[i];j++){
                String word = s.substring(j,i);
                if(set.contains(word) && valid[j]){
                    valid[i] = true;
                }
            }

        }
        return valid[s.length()];
    }
}
